<?php
/**
 * Autores:Daniel sanchez merlo,Guillermo Rodriguez congosto
 * Asignatura:GIW
 * GRUPO:A
 * Este código es exclusivamente de nuestra propiedad
 */
?>
<?php

session_start ();

if (isset ( $_POST ['logout'] )) {
	unset ( $_SESSION ['id'] );
	unset ( $_SESSION ['rol'] );
	session_destroy ();
}

?>
<HTML>
<HEAD>
<TITLE>Práctica-Teatros</TITLE>
<meta charset="utf-8">
<STYLE TYPE="text/css">
<!--
input {
	font-family: Arial, Helvetica;
	font-size: 14;
	color: #000033;
	font-weight: normal;
	border-color: #999999;
	border-width: 1;
	background-color: #FFFFFF;
}

form {
	display: inline;
}

th {
	background: green;
	color: white;
	padding: 10px;
}
-->
</style>
</HEAD>

<BODY bgcolor="#C0C0C0" link="green" vlink="green" alink="green">
	<BASEFONT face="arial, helvetica">

	<TABLE border="0" align="center" cellspacing="3" cellpadding="3"
		width="1000">
		<TR>
			<TH colspan="2" width="100%" bgcolor="green"><FONT size="6"
				color="white">Teatros</FONT></TH>
		</TR>
	</TABLE>
	<P>

<?php
// conectar
$conexion = new MongoClient ();
$bd = $conexion->GIW_grupo11;

include 'DBHelper.php';
include 'Inputs.php';
include 'usuario.php';

$usuario = new usuario ( $bd );
// $_SESSION['nick']="prueba";
if (! isset ( $_SESSION ['id'] )) {
	
	if (isset ( $_POST ['nick'] ) && isset ( $_POST ['password'] )) {
		
		try {
			Inputs::validate_usuario_login ( $_POST ['nick'], $_POST ['password'] );
			if ($usuario->comprobar_usuario ( $_POST ['nick'], $_POST ['password'] )) {
				
				header ( "Location: index2.php" );
			} else {
				$error = "usuario o contraseña incorrectos";
			}
		} catch ( Exception $e ) {
			$error = "campos introducidos no validos";
		}
	}
	
	if (isset ( $_POST ['registro'] )) {
		include 'registro.php';
	} else if (isset ( $_POST ['registro_usuario'] )) {
		if (isset ( $_POST ['dni'] ) && isset ( $_POST ['nombre'] ) && isset ( $_POST ['apellidos'] ) && isset ( $_POST ['newpassword'] ) && isset ( $_POST ['newnick'] ) && isset ( $_POST ['email'] ) && isset ( $_POST ['rol'] )) {
			/*
			 * if ($usuario->guardar_usuario ( $_POST ['dni'], $_POST ['nombre'], $_POST ['apellidos'], $_POST ['newnick'], $_POST ['newpassword'], $_POST ['email'], $_POST ['rol'] )) {
			 * include "registro_ok.php";
			 * }
			 */
			try {
				Inputs::validate_usuario_registro ( $_POST ['dni'], $_POST ['nombre'], $_POST ['apellidos'], $_POST ['email'], $_POST ['newnick'], $_POST ['newpassword'], $_POST ['rol'] );
				$usuario->guardar_usuario ( $_POST ['dni'], $_POST ['nombre'], $_POST ['apellidos'], $_POST ['newnick'], $_POST ['newpassword'], $_POST ['email'], $_POST ['rol'] );
				include "registro_ok.php";
			} catch ( Exception $e ) {
				include "registro_fallo.php";
			}
		}
	} else {
		include 'login.php';
	}
} else {
	
	include "teatro2.php";
	
	?>

	
	
	
	
	<form method="post" action="index2.php">
		<input type="submit" name="logout" value="logout">
	</form>
	<form method="post" action="index2.php?operacion=editar_perfil">
		<input type="submit" name="editar_perfil" value="editar perfil">
	</form>
				<?php if($_SESSION['rol']=="cliente"){?>
	
					<form method="post" action="index2.php?operacion=ver_entradas">
		<input type="submit" name="ver_entradas"
			value="Ver entradas compradas">
	</form>
				<?php }?>
	
	<?php
	if (isset ( $_REQUEST ['operacion'] ))
		$operacion = $_REQUEST ['operacion'];
	echo "<CENTER><P>
	  <TABLE border='0' width='600'>
	  <TR>
	     <TD valign=top align=CENTER colspan=2>";
	if ($_SESSION ['rol'] == "cliente") {
		echo "<FORM name='form1' METHOD='POST' ACTION='index2.php?operacion=buscar'>
	     	    <FONT size ='-1'>Buscar teatro </FONT>";
		if (! isset ( $_POST ['busqueda'] )) {
			$busqueda = "";
		} else {
			$busqueda = $_POST ['busqueda'];
		}
		echo "<INPUT TYPE='TEXT' NAME='busqueda' value='$busqueda' size='20'> ";
		echo "<INPUT TYPE='SUBMIT' NAME='boton_buscar' VALUE='¡Buscar!'>
	     	</FORM>";
	}
	echo " </TD></TR><TR><TD align=right>";
	if ($_SESSION ['rol'] == "administrador") {
		echo "<FORM name='form2' METHOD='POST' ACTION='index2.php?operacion=introducir'>
	            <INPUT TYPE='SUBMIT' NAME='alta' VALUE='Nuevo teatro'>
	        </FORM>";
	}
	echo "</TD><TD width=100 align=left>
	        <FORM name='form3' METHOD='POST' ACTION='index2.php?operacion=listado'>
	            <INPUT TYPE='SUBMIT' NAME='alta' VALUE='Listado completo'>
	        </FORM>
	     </TD>
	  </TR></TABLE>";
	if ($_SESSION ['rol'] == "cliente") {
		if ($busqueda != "") {
			echo "Resultado para la busqueda: " . $busqueda;
		}
	}
	
	$teatro = new teatro ( $bd );
	
	if (isset ( $operacion )) {
		if ($operacion == "listado") {
			
			if ((isset ( $_POST ['consulta'] ) && ! empty ( $_POST ['id'] )) || isset ( $_POST ['cambiar_sesion_admin'] )) {
				
				if (! isset ( $_POST ['id'] )) {
					$_POST ['id'] = $_POST ['Id_teatro'];
				}
				
				$sesion = "1";
				$day = date ( "d-m-Y" );
				if (isset ( $_POST ['sesion'] )) {
					$sesion = $_POST ['sesion'];
				}
				
				if (isset ( $_POST ['day'] )) {
					$day = $_POST ['day'];
				}
				
				$orden = "fecha";
				if (isset ( $_POST ['puntuacion'] )) {
					$orden = "puntuacion";
				}
				try{
					Inputs::validate_obra_recuperar_obra($_POST ['id'], $orden, $sesion, $day);
					$teatro->recuperar_teatro ( $_POST ['id'], $orden, $sesion, $day );
				} catch (Exception $e){
					$error="consulta no valida";
				}
			} else if (isset ( $_POST ['modificar'] ) && ! empty ( $_POST ['id'] )) {
				// $teatro-> modificar($_POST['Id'], $_POST['nombre_obra'], $_POST['nombre_teatro'], $_POST['descripcion'], $_POST['sesion1'], $_POST['sesion2'], $_POST['sesion3'], $_POST['nume_filas'], $_POST['nume_asientos']);
				try{
					Inputs::validate_obra_id( $_POST ['id']);
					$teatro->recuperar_teatroModificar ( $_POST ['id'] );
				} catch (Exception $e){
					$error="consulta no válida";
				}
			} else if (isset ( $_POST ['actualiza'] ) && ! empty ( $_POST ['id'] )) {
				try {
					Inputs::validate_obra_editar ( $_POST ['id'], $_POST ['nombre_teatro'], $_POST ['nombre_obra'], $_POST ['descripcion'], $_POST ['sesion1'], $_POST ['sesion2'], $_POST ['sesion3'], $_POST ['nume_filas'], $_POST ['nume_asientos'] );
					
					$teatro->modificar ( $_POST ['id'], $_POST ['nombre_obra'], $_POST ['nombre_teatro'], $_POST ['descripcion'], $_POST ['sesion1'], $_POST ['sesion2'], $_POST ['sesion3'], $_POST ['nume_filas'], $_POST ['nume_asientos'] );
				} catch ( Exception $e ) {
					$error = "campos introducidos no válidos";
				}
			} else if (isset ( $_POST ['eliminar'] ) && ! empty ( $_POST ['id'] )) {
				try {
					Inputs::validate_obra_id ( $_POST ['id'] );
					$teatro->del_teatro ( $_POST ['id'] );
				} catch ( Exception $e ) {
				}
			} else if ((isset ( $_POST ['comprar'] ) && ! empty ( $_POST ['id'] )) || isset ( $_POST ['comprar_sesion'] ) || isset ( $_POST ['borrar_sesion'] ) || isset ( $_POST ['cambiar_sesion'] )) {
				
				if (! isset ( $_POST ['id'] )) {
					$_POST ['id'] = $_POST ['Id_teatro'];
				}
				
				$sesion = "1";
				$day = date ( "d-m-Y" );
				if (isset ( $_POST ['sesion'] )) {
					$sesion = $_POST ['sesion'];
				}
				
				if (isset ( $_POST ['day'] )) {
					$day = $_POST ['day'];
				}
				if (isset ( $_POST ['comprar_sesion'] )) {
					try{
						Inputs::validate_entradas_comprar($_POST ['Id_teatro'], $_POST ['sesion'], $_POST ['fila'], $_POST ['asiento'], $_POST ['dia']);
						$teatro->exec_comprar ( $_POST ['Id_teatro'], $_POST ['sesion'], $_POST ['fila'], $_POST ['asiento'], true, $_POST ['dia'] );
					} catch (Exception $e){
						$error="datos de compra no validos";
					}
				}
				if (isset ( $_POST ['borrar_sesion'] )) {
					try{
						Inputs::validate_entradas_comprar($_POST ['Id_teatro'], $_POST ['sesion'], $_POST ['fila'], $_POST ['asiento'], $_POST ['dia']);
						$teatro->exec_comprar ( $_POST ['Id_teatro'], $_POST ['sesion'], $_POST ['fila'], $_POST ['asiento'], false, $_POST ['dia'] );
					} catch (Exception $e){
						$error="datos de cancelación no validos";
					}
				}
				
				try{
					Inputs::validate_entradas_consultar($_POST ['id'], $sesion, $day);
					$teatro->comprar ( $_POST ['id'], $sesion, $day );
				} catch (Exception $e){
					$error="consulta de entradas no valida";
				}
			} 

			else {
				$teatro->listar ();
			}
		} else if ($operacion == "buscar") {
			$teatro->buscar ( $_POST ['busqueda'] );
		} else if ($operacion == "introducir") {
			
			if (isset ( $_POST ['nuevo'] )) {
				// print_r($_POST);
				try {
					Inputs::validate_obra_nueva ( $_POST ['nombre_teatro'], $_POST ['nombre_obra'], $_POST ['descripcion'], $_POST ['sesion1'], $_POST ['sesion2'], $_POST ['sesion3'], $_POST ['nume_filas'], $_POST ['nume_asientos'] );
					
					$teatro->add_teatro ( $_POST ['nombre_teatro'], $_POST ['nombre_obra'], $_POST ['descripcion'], $_POST ['sesion1'], $_POST ['sesion2'], $_POST ['sesion3'], $_POST ['nume_filas'], $_POST ['nume_asientos'] );
				} catch ( Exception $e ) {
					$error = "campos introducidos no válidos";
				}
			} else {
				$teatro->datos_teatro ();
			}
		} else if ($operacion == "editar_perfil") {
			if (isset ( $_POST ['actualizar_perfil'] )) {
				try {
					Inputs::validate_usuario_editar ( $_POST ['nombre'], $_POST ['apellidos'], $_POST ['email'], $_POST ['nick'], $_POST ['password'] );
					if ($usuario->actualizar_usuario ( $_SESSION ['id'], $_POST ['nombre'], $_POST ['apellidos'], $_POST ['nick'], $_POST ['password'], $_POST ['email'] )) {
						echo "El usuario se ha actualizado correctamente";
					} else {
						$error = "error al actualizar";
					}
				} catch ( Exception $e ) {
					$error = "campos introducidos no válidos";
				}
			} else {
				$usuario->editar_usuario ();
			}
		} else if ($operacion == "ver_entradas") {
			if (isset ( $_POST ['valorar'] )) {
				try {
					Inputs::validate_obra_valoracion ( $_POST ['id'], $_POST ['puntuacion'], $_POST ['comentario'] );
					$usuario->valorar_obra ( $_POST ['id'], $_POST ['puntuacion'], $_POST ['comentario'] );
				} catch ( Exception $e ) {
					$error = "Valoracion no válida";
				}
			} else {
				$usuario->ver_entradas ();
			}
		}
	} else {
		$teatro->listar ();
	}
}
if(isset($error)){
	echo $error;
}
?>

</BODY>
</HTML>